package com.woniuxy.web;

import com.woniuxy.entity.*;
import com.woniuxy.service.AddressService;
import com.woniuxy.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @Autowired
    private RegionService regionService;
    //进入个人中心的收货地址
    @RequestMapping("list")
    public String toMyAddress(){
        return "ucenter/user_address";
    }

    @GetMapping("show")
    @ResponseBody
    public ResponseEntity<List<Address>> show(HttpSession session) {
        User user = (User) session.getAttribute("user");
        /*ResponseEntity<List<Address>> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setData(addressService.getByAddr(user.getId()));
        return responseEntity;*/
        List<Address> addresses = addressService.getByAddr(user.getId());
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("ok");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(addresses);
        return responseEntity;
    }


    @PostMapping("add")
    @ResponseBody
    public ResponseEntity add(@RequestBody Address address, HttpSession session){
            User user = (User) session.getAttribute("user");
             ResponseEntity responseEntity = new ResponseEntity();
            if(addressService.getCountByUserId(user.getId())>4){
                return ResponseEntity.FIAL;
            }
            if(address.getStatus().equals("y")){
                Address add = addressService.getDefault(user.getId(),"y");
                add.setStatus("n");
                addressService.updateById(add);
            } else if (address.getStatus() == null || address.getStatus() ==""){
                address.setStatus("n");
            }
            address.setUserid(user.getId());
            String province = regionService.getRegionById(address.getProvinceId());
            String city = regionService.getRegionById(address.getCityId());
            String district =regionService.getRegionById(address.getDistrictId());
            address.setProvince(province);
            address.setCity(city);
            address.setDistrict(district);
            addressService.save(address);
            responseEntity.setMsg("ok");
            responseEntity.setCode(ResponseState.SUCCESS);
            responseEntity.setData(address);
            return responseEntity;
    }

    @GetMapping("del")
    @ResponseBody
    public ResponseEntity delete(@RequestParam int id,HttpSession session){
        User user = (User) session.getAttribute("user");
        ResponseEntity responseEntity = new ResponseEntity();
        addressService.removeById(id);
        responseEntity.setMsg("ok");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(addressService.getByAddr(user.getId()));
        return responseEntity;
    }

    @GetMapping("initUpdate")
    @ResponseBody
    public ResponseEntity update(@RequestParam int id){
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("ok");
        responseEntity.setCode(ResponseState.SUCCESS);
        Address address = addressService.getById(id);
        responseEntity.setData(address);
        return responseEntity;
    }

    @PostMapping("update")
    @ResponseBody
    public ResponseEntity update(@RequestBody Address address,HttpSession session){
        ResponseEntity responseEntity = new ResponseEntity();
        User user = (User) session.getAttribute("user");
        if(address.getStatus().equals("y")){
            Address address1 = addressService.getDefault(user.getId(),"y");
            address1.setStatus("n");
            addressService.updateById(address1);
        }
        String province = regionService.getRegionById(address.getProvinceId());
        String city = regionService.getRegionById(address.getCityId());
        String district = regionService.getRegionById(address.getDistrictId());
        address.setUserid(user.getId());
        address.setProvince(province);
        address.setCity(city);
        address.setDistrict(district);
        addressService.updateById(address);
        List<Address> addresses = addressService.getByAddr(user.getId());
        responseEntity.setMsg("ok");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(addresses);
        return responseEntity;
    }

    @GetMapping("setdefault")
    @ResponseBody
    public ResponseEntity setDefault(@RequestParam int id,HttpSession session){
        User user = (User) session.getAttribute("user");
        Address address1 = addressService.getDefault(user.getId(),"y");
        if(address1 != null){
            address1.setStatus("n");
            addressService.updateById(address1);
        }
        Address address = addressService.getById(id);
        address.setStatus("y");
        addressService.updateById(address);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("ok");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(address);
        return responseEntity;
    }
}
